var now = new Date() //当前日期
var nowDayOfWeek = now.getDay() //今天本周的第几天
var nowDay = now.getDate() //当前日
var nowMonth = now.getMonth() //当前月
var nowYear = now.getFullYear() //当前年
//获得本周的开端日期
function getWeekStartDate() {
  let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek)
  return formatDate(weekStartDate)
}
//获得本周的停止日期
function getWeekEndDate() {
  let weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek))
  return formatDate(weekEndDate)
}
//获得上周的开端日期
function getBeforeWeekStartDate() {
  let weekStartDate = new Date(nowYear, nowMonth, nowDay - 7 - nowDayOfWeek)
  return formatDate(weekStartDate)
}
//获得上周的停止日期
function getBeforeWeekEndDate() {
  let weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek - 7))
  return formatDate(weekEndDate)
}
//获得本月的开端日期
function getMonthStartDate() {
  let monthStartDate = new Date(nowYear, nowMonth, 1)
  return formatDate(monthStartDate)
}
//获得本月的停止日期
function getMonthEndDate() {
  let monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth))
  return formatDate(monthEndDate)
}
//获得上月开端时候
function getLastMonthStartDate() {
  let lastMonthStartDate = new Date(new Date().getFullYear(), new Date().getMonth() - 1, 1)
  return formatDate(lastMonthStartDate)
}
//获得上月停止时候
function getLastMonthEndDate() {
  var date = new Date()
  var day = new Date(date.getFullYear(), date.getMonth(), 0).getDate()
  var lastMonthEndDate = new Date(new Date().getFullYear(), new Date().getMonth() - 1, day)
  return formatDate(lastMonthEndDate)
}
//获得本年、去年、明年的开端日期，结束日期
function getYear(type, dates) {
  var dd = new Date()
  var n = dates || 0
  var year = dd.getFullYear() + Number(n)
  if (type == 's') {
    var day = year + '-01-01'
  }
  if (type == 'e') {
    var day = year + '-12-31'
  }
  if (!type) {
    var day = year + '-01-01/' + year + '-12-31'
  }
  return day
}

/* getYear(type,dates)  //type为字符串类型，有两种选择，"s"代表开始,"e"代表结束，dates为数字类型，不传或0代表今年，-1代表去年，1代表明年
getYear("s",1)  //得到明年第一天的yyyy-mm-dd格式日期
getYear("e",1)  //得到明年最后一天的yyyy-mm-dd格式日期 */

//获得某月的天数
function getMonthDays(myMonth) {
  let monthStartDate = new Date(nowYear, myMonth, 1)
  let monthEndDate = new Date(nowYear, myMonth + 1, 1)
  let days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24)
  return days
}
//字段拼接
function substrDataStr(data) {
  let strArr = []
  strArr.push(data.substr(0, 4))
  strArr.push(data.substr(4, 2))
  strArr.push(data.substr(6, 2))
  return strArr
}

function format(shijianchuo) {
  //shijianchuo是整数，否则要parseInt转换
  if (!shijianchuo) {
    return ''
  }
  if (shijianchuo.toString().length < 13) {
    shijianchuo = shijianchuo * 1000
  }
  var time = new Date(shijianchuo)
  var y = time.getFullYear()
  var m = time.getMonth() + 1
  var d = time.getDate()
  var h = time.getHours()
  var mm = time.getMinutes()
  var s = time.getSeconds()
  if (m < 10) {
    m = '0' + m
  }
  if (d < 10) {
    d = '0' + d
  }
  if (h < 10) {
    h = '0' + h
  }
  if (mm < 10) {
    mm = '0' + mm
  }
  if (s < 10) {
    s = '0' + s
  }
  let result = y + '-' + m + '-' + d + ' ' + h + ':' + mm + ':' + s
  return result
}
//格局化日期：yyyy-MM-dd
function formatDate(date) {
  let myyear = date.getFullYear()
  let mymonth = date.getMonth() + 1
  let myweekday = date.getDate()
  if (mymonth < 10) {
    mymonth = '0' + mymonth
  }
  if (myweekday < 10) {
    myweekday = '0' + myweekday
  }
  return myyear + '/' + mymonth + '/' + myweekday
}

function formatDate1(shijianchuo) {
  if (shijianchuo && shijianchuo.toString().length < 13) {
    shijianchuo = shijianchuo * 1000
  }
  let date = new Date(shijianchuo)
  let myyear = date.getFullYear()
  let mymonth = date.getMonth() + 1
  let myweekday = date.getDate()
  if (mymonth < 10) {
    mymonth = '0' + mymonth
  }
  if (myweekday < 10) {
    myweekday = '0' + myweekday
  }
  return myyear + '.' + mymonth + '.' + myweekday
}
//格局化日期：yyyy-MM-dd
function formatDateHMS(date) {
  let myyear = date.getFullYear()
  let mymonth = date.getMonth() + 1
  let myweekday = date.getDate()
  let hour = date.getHours()
  let minute = date.getMinutes()
  let second = date.getSeconds()
  if (mymonth < 10) {
    mymonth = '0' + mymonth
  }
  if (myweekday < 10) {
    myweekday = '0' + myweekday
  }
  if (hour < 10) {
    hour = '0' + hour
  }
  if (minute < 10) {
    minute = '0' + minute
  }
  if (second < 10) {
    second = '0' + second
  }
  return myyear + '-' + mymonth + '-' + myweekday + ' ' + hour + ':' + minute + ':' + second
}
//格局化日期：时分秒
function formatHMS() {
  var date = new Date()
  let hour = date.getHours()
  let minute = date.getMinutes()
  let second = date.getSeconds()

  return ' ' + hour + ':' + minute + ':' + second
}
//获取今天之前的多少天的日期
function GetDateStr(AddDayCount) {
  let dd = new Date()
  dd.setDate(dd.getDate() - AddDayCount) //获取AddDayCount天后的日期
  let y = dd.getFullYear()
  let m = dd.getMonth() + 1 //获取当前月份的日期
  let d = dd.getDate()
  if (m < 10) {
    m = '0' + m
  }
  if (d < 10) {
    d = '0' + d
  }
  return y + '-' + m + '-' + d
}
// 获取指定某一天的前几天的日期或者后几天的日期
function getNextDate(date, day) {
  var dd = new Date(date)
  dd.setDate(dd.getDate() + day)
  var y = dd.getFullYear()
  var m = dd.getMonth() + 1 < 10 ? '0' + (dd.getMonth() + 1) : dd.getMonth() + 1
  var d = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate()
  return y + '-' + m + '-' + d
}

// 获取某月的天数 getDaysInOneMonth(new Date('2019/1/1')
function getDaysInOneMonth(date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const d = new Date(year, month, 0)
  return d.getDate()
}

// 获取当前时间
export function getTime() {
  var date = new Date()
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  var minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  var second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  month >= 1 && month <= 9 ? (month = '0' + month) : ''
  day >= 0 && day <= 9 ? (day = '0' + day) : ''
  var timer = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
  return timer
}

// 输入时间格式获取字符串 默认 YYYY-MM-dd
export function dateStrFromDate(date, format) {
  if (!format) {
    format = 'YYYY-MM-dd'
  }
  let time = {
    'M+': date.getMonth() + 1,
    'd+': date.getDate(),
    'h+': date.getHours(),
    'm+': date.getMinutes(),
    's+': date.getSeconds(),
    'q+': Math.floor((date.getMonth() + 3) / 3),
    'S+': date.getMilliseconds(),
  }
  if (/(y+)/i.test(format)) {
    format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (let k in time) {
    if (new RegExp('(' + k + ')').test(format)) {
      format = format.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? time[k] : ('00' + time[k]).substr(('' + time[k]).length)
      )
    }
  }
  return format
}

// 获取当前时间的年月日
export function getTimeYMD() {
  var date = new Date()
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  month >= 1 && month <= 9 ? (month = '0' + month) : ''
  day >= 0 && day <= 9 ? (day = '0' + day) : ''
  var timer = year + '-' + month + '-' + day
  return timer
}

// 获取当前时间的时分秒
export function getTimeHMS() {
  var date = new Date()
  var hour = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
  var minute = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
  var second = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
  var timer = hour + ':' + minute + ':' + second
  return timer
}

export default {
  getWeekStartDate,
  getWeekEndDate,
  getBeforeWeekStartDate,
  getBeforeWeekEndDate,
  getMonthStartDate,
  getMonthEndDate,
  getLastMonthStartDate,
  getLastMonthEndDate,
  substrDataStr,
  formatDate,
  formatDate1,
  formatDateHMS,
  GetDateStr,
  getDaysInOneMonth,
  getTime,
  getTimeYMD,
  getTimeHMS,
  format,
  getNextDate,
  getYear,
  formatHMS,
  dateStrFromDate,
}

